//-------------------------------
// Window_RecipeItem
//-------------------------------


Window_RecipeItem.prototype.initialize = function(x, y ,width, height, baseScene){
	Window_Selectable.prototype.initialize.call(this, x, y, width, height);

	this._baseScene = baseScene;

	this._viewerWindow1 = new Window_ItemViewer(this.contents.width / 2 - 216 - 96, 108, 240, 334)
	this._viewerWindow2 = new Window_ItemViewer(this.contents.width / 2 + 216 - 96, 108, 240, 334)
	this._viewerWindow3 = new Window_ItemViewer(this.contents.width / 2 - 96, 108, 240, 334)
	this.addChild(this._viewerWindow1);
	this.addChild(this._viewerWindow2);
	this.addChild(this._viewerWindow3);
	this._viewerWindow1.hide();
	this._viewerWindow2.hide();
	this._viewerWindow3.hide();
	this._viewerWindow3.opacity = 0;
	this._viewerWindow3._itemPreview.opacity = 0;

	this.createCursor();


	this.refresh();
	this.activate();
	this.select(0);
	this.cursorOpacity = 0;
	this._cursorAnimationCount = 0;
	this._requestSysAnimation = false;
	this._sysAnimationCount = 0;
	this._isSysAnimating = false;
};

Window_RecipeItem.prototype.createCursor = function(){
	this._cursor = new Sprite();
	this._cursor.bitmap = ImageManager.loadBitmap('img/system/UI/', 'Cursor_Synthesis');
	this._cursor.anchor.x = 0.5;
	this._cursor.anchor.y = 1;
	this._cursor.y = 107;
	this._cursor.visible = false;
	this.addChild(this._cursor);
}

let SWDA_Window_RecipeItem_update = Window_RecipeItem.prototype.update;
Window_RecipeItem.prototype.update = function() {
	SWDA_Window_RecipeItem_update.call(this);
	if(!this.active){
		this._cursor.visible = false;
	}else{
		this._cursor.visible = true;
		this.updateCursorY(this.index());
	}

	if (this._requestSysAnimation){
		this.updateSysAnimation();
	}

};
		


Window_RecipeItem.prototype.updateSysAnimation = function(){
	//const animationId = 59;
	const aniDur1 = 60;	//汇聚
	const aniDur2 = 30;	//融合
	const aniDur3 = 30;	//等待
	const aniDur4 = 10;	//结果淡出
	const dx = 216;
	if(this._sysAnimationCount == 0){//初始
		this.clearItemInfo(0);
		this.clearItemInfo(1);
		AudioManager.playSe(this.sysSound());
	}else if(this._sysAnimationCount < aniDur1){//汇聚
		this._viewerWindow1.x += dx/aniDur1;
		this._viewerWindow2.x -= dx/aniDur1;
		if(this._sysAnimationCount > aniDur1/2){
			this._viewerWindow1.opacity -= 255/(aniDur1);
			this._viewerWindow2.opacity -= 255/(aniDur1);
			this._viewerWindow1._itemPreview.opacity = this._viewerWindow1.opacity;
			this._viewerWindow2._itemPreview.opacity = this._viewerWindow2.opacity;
		}
	}else if(this._sysAnimationCount == aniDur1){//汇聚结束，播放音效、显示结果
		if(!!this._baseScene._finalResult[0])
			SoundManager.playUseItem();
		else
			SoundManager.playBuzzer();
		this._viewerWindow1.x = this.contents.width / 2 - 96;
		this._viewerWindow2.x = this.contents.width / 2 - 96;
		// 显示帮助窗口
		this._baseScene.showResultInfo(this._baseScene._finalResult[2],this._baseScene._finalResult[3]);
		this._baseScene._helpWindow.show();

	}else if(this._sysAnimationCount < aniDur1 + aniDur2){//融合，淡入产物图
		this._viewerWindow1.opacity -= 255/2/(aniDur2);
		this._viewerWindow2.opacity -= 255/2/(aniDur2);
		this._viewerWindow3.opacity += 255/(aniDur2);
		this._viewerWindow1._itemPreview.opacity = this._viewerWindow1.opacity;
		this._viewerWindow2._itemPreview.opacity = this._viewerWindow2.opacity;
		this._viewerWindow3._itemPreview.opacity = this._viewerWindow3.opacity;
	}else if(this._sysAnimationCount == aniDur1 + aniDur2){//融合完成
		this._viewerWindow1.opacity = 0;
		this._viewerWindow2.opacity = 0;
		this._viewerWindow3.opacity = 255;
	}else if(this._sysAnimationCount < aniDur1 + aniDur2 + aniDur3){
		//等待
	}else if(this._sysAnimationCount < aniDur1 + aniDur2 + aniDur3 + aniDur4){//淡出
		this._viewerWindow3.opacity -= 255/(aniDur2/2);
		this._viewerWindow3._itemPreview.opacity = this._viewerWindow3.opacity;
	}else if(this._sysAnimationCount == aniDur1 + aniDur2 + aniDur3 + aniDur4){
		this.endSysAnimation();
	}

	this._sysAnimationCount++;
	
}


Window_RecipeItem.prototype.endSysAnimation = function(){
	this._isSysAnimating = false;
	this._sysAnimationCount = 0;
	this._requestSysAnimation = false;
	this._baseScene.setHelpWindowText('default');
	this.refresh();
	this._viewerWindow1.x = this.contents.width / 2 - 216 - 96;
	this._viewerWindow2.x = this.contents.width / 2 + 216 - 96;
	this._viewerWindow1.opacity = 255;
	this._viewerWindow2.opacity = 255;
	this._viewerWindow3.opacity = 0;
	this._viewerWindow1._itemPreview.opacity = 255;
	this._viewerWindow2._itemPreview.opacity = 255;
	this._viewerWindow3._itemPreview.opacity = 0;
	this._viewerWindow3.hide();
	this._baseScene._resultWindow.initData();
	this.activate();
	this.select(0);

}

Window_RecipeItem.prototype.requestSysAnimation = function(){
	this._viewerWindow3.showView(this._baseScene._finalResult[1]);
	this._viewerWindow3.opacity = 0;
	this._viewerWindow3._itemPreview.opacity = 0;
	this._requestSysAnimation = true;
	this._isSysAnimating = true;
	this._sysAnimationCount = 0;
	//this._viewerWindow3.show();
}
Window_RecipeItem.prototype.isSysAnimating = function(){
	return this._isSysAnimating;
}


Window_RecipeItem.prototype.sysSound = function () {
    const obj = {
        name: 'sys_pot1',
		volume: 90,
		pitch: 100,
		pan: 0,
    }
    return obj;
};

Window_RecipeItem.prototype.setCursorX = function(index){
	this._cursor.x = Graphics.boxWidth / 2 + 216 * (index * 2 - 1);
}

Window_RecipeItem.prototype.updateCursorY = function(){
	this._cursor.y = 107 - 4 + 8 * Math.sin(this._cursorAnimationCount % 80 / 80 * 2 * Math.PI);
	this._cursorAnimationCount++;
}

Window_RecipeItem.prototype.itemRect = function(index){
	const rect = Window_Selectable.prototype.itemRect.call(this, index + this.maxCols());
	rect.y = 83;
	rect.x = this.contents.width / 2 + 216 * (index * 2 - 1) - 120;

	rect.width = 240;
	rect.height = 336;

	return rect;
};

Window_RecipeItem.prototype.itemInfoRect = function(index){
	const rect = this.itemRect(index);
	rect.y = rect.y + rect.height + 24;
	rect.x = this.contents.width / 2 + 216 * (index * 2 - 1) - 120;
	rect.width = 240;
	rect.height = 96;

	return rect;
};

Window_Selectable.prototype.clearItemInfo = function(index) {
    const rect = this.itemInfoRect(index);
    this.contents.clearRect(rect.x, rect.y, rect.width, rect.height);
};


let SWDA_Window_RecipeItem_processCursorMove = Window_RecipeItem.prototype.processCursorMove;
Window_RecipeItem.prototype.processCursorMove = function() {
	SWDA_Window_RecipeItem_processCursorMove.call(this);
	this.setCursorX(this.index());
};


Window_RecipeItem.prototype.drawAllItems = function(){
	Window_Selectable.prototype.drawAllItems.call(this);
};

Window_RecipeItem.prototype.drawItem = function(index){
	const infoRect = this.itemInfoRect(index);
	if(index % 2 === 0){
		if(!!this.leftRecipeItem()){
			this._viewerWindow1.showView(this.leftRecipeItem());
			this.drawItemInfo(infoRect, this.leftRecipeItem());
		}else{
			this._viewerWindow1.hide();
		}
	}else{
		if(!!this.rightRecipeItem()){
			this._viewerWindow2.showView(this.rightRecipeItem());
			this.drawItemInfo(infoRect, this.rightRecipeItem());
		}else{
			this._viewerWindow2.hide();
		}
	}
};




Window_RecipeItem.prototype.drawItemInfo = function(rect, item){
	let x = rect.x;
	let y = rect.y;
	let level = 0;
	
	const iconBoxWidth = Window_Base._iconWidth;
	this.resetFontSettings();
	this.resetTextColor();
	this.drawIcon(item.iconIndex, x, y + 2);
	this.drawText(item.name.split("（")[0], x + iconBoxWidth, y, rect.width - 32, "left");

	//y += this.lineHeight();
	level = item.catchLevel;
	if (item.useItemId){
		if($dataItems[item.useItemId].petAddedInfo){
			level = $dataEnemies[$dataItems[item.useItemId].petAddedInfo.enemyId].catchLevel;
		}
	}

	x = x + rect.width - 32 - this.contents.fontSize - 2;
	this.setSmallerFont();
	this.drawText(level, x, y, this.textWidth('00'),'right');
	this.resetFontSettings();
	this.drawText(TextManager.levelA, rect.x + this.textWidth('00'), y, rect.width - 32, "right");
};
